package com.ruoyi.quanzi.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.quanzi.mapper.SunPostCollectionMapper;
import com.ruoyi.quanzi.domain.SunPostCollection;
import com.ruoyi.quanzi.service.ISunPostCollectionService;

/**
 * 帖子收藏服务实现类
 * 
 * @author ruoyi
 * @date 2024-01-01
 */
@Service
public class SunPostCollectionServiceImpl implements ISunPostCollectionService
{
    @Autowired(required = false)
    private SunPostCollectionMapper sunPostCollectionMapper;

    /**
     * 查询帖子收藏
     * 
     * @param id 帖子收藏ID
     * @return 帖子收藏
     */
    @Override
    public SunPostCollection selectSunPostCollectionById(Long id)
    {
        if (sunPostCollectionMapper != null) {
            return sunPostCollectionMapper.selectSunPostCollectionById(id);
        }else{
            return new SunPostCollection();
        }
        

    }

    /**
     * 查询帖子收藏列表
     * 
     * @param sunPostCollection 帖子收藏
     * @return 帖子收藏集合
     */
    @Override
    public List<SunPostCollection> selectSunPostCollectionList(SunPostCollection sunPostCollection)
    {
        if (sunPostCollectionMapper != null) {
            return sunPostCollectionMapper.selectSunPostCollectionList(sunPostCollection);
        }else{
            return new ArrayList<>();
        }


    }

    /**
     * 新增帖子收藏
     * 
     * @param sunPostCollection 帖子收藏
     * @return 结果
     */
    @Override
    public int insertSunPostCollection(SunPostCollection sunPostCollection)
    {
        if (sunPostCollectionMapper != null) {
            sunPostCollection.setCreateTime(new Date());
            return sunPostCollectionMapper.insertSunPostCollection(sunPostCollection);
        }
        return 1;
    }

    /**
     * 修改帖子收藏
     * 
     * @param sunPostCollection 帖子收藏
     * @return 结果
     */
    @Override
    public int updateSunPostCollection(SunPostCollection sunPostCollection)
    {
        if (sunPostCollectionMapper != null) {
            sunPostCollection.setUpdateTime(new Date());
            return sunPostCollectionMapper.updateSunPostCollection(sunPostCollection);
        }
        return 1;
    }

    /**
     * 批量删除帖子收藏
     * 
     * @param ids 需要删除的帖子收藏ID
     * @return 结果
     */
    @Override
    public int deleteSunPostCollectionByIds(Long[] ids)
    {
        if (sunPostCollectionMapper != null) {
            return sunPostCollectionMapper.deleteSunPostCollectionByIds(ids);
        }
        return ids.length;
    }

    /**
     * 删除帖子收藏信息
     * 
     * @param id 帖子收藏ID
     * @return 结果
     */
    @Override
    public int deleteSunPostCollectionById(Long id)
    {
        if (sunPostCollectionMapper != null) {
            return sunPostCollectionMapper.deleteSunPostCollectionById(id);
        }
        return 1;
    }

    /**
     * 用户收藏帖子
     * 
     * @param userId 用户ID
     * @param postId 帖子ID
     * @return 结果
     */
    @Override
    public int collectPost(Long userId, Long postId)
    {
        if (sunPostCollectionMapper != null) {
            return sunPostCollectionMapper.collectPost(userId, postId);
        }
        return 1;
    }

    /**
     * 用户取消收藏帖子
     * 
     * @param userId 用户ID
     * @param postId 帖子ID
     * @return 结果
     */
    @Override
    public int uncollectPost(Long userId, Long postId)
    {
        if (sunPostCollectionMapper != null) {
            return sunPostCollectionMapper.uncollectPost(userId, postId);
        }
        return 1;
    }

    /**
     * 根据用户ID查询收藏列表
     * 
     * @param userId 用户ID
     * @return 收藏集合
     */
    @Override
    public List<SunPostCollection> selectCollectionsByUserId(Long userId)
    {
        if (sunPostCollectionMapper != null) {
            return sunPostCollectionMapper.selectCollectionsByUserId(userId);
        }else{
            return new ArrayList<>();
        }
    }

    /**
     * 检查用户是否收藏了某个帖子
     * 
     * @param userId 用户ID
     * @param postId 帖子ID
     * @return 是否收藏
     */
    @Override
    public boolean isPostCollectedByUser(Long userId, Long postId)
    {
        if (sunPostCollectionMapper != null) {
            return sunPostCollectionMapper.isPostCollectedByUser(userId, postId) > 0;
        }
        return false;
    }
}